home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2006 May / PCWMAY06.iso / Software / Toolkit / Songbird 0.1 / Songbird_0_1_0.exe / components / sbIPlaylistManager.js < prev    next >
Text File  |  2006-02-07  |  30KB  |  879 lines

  1. /*
  2.  //
  3. // BEGIN SONGBIRD GPL
  4. // 
  5. // This file is part of the Songbird web player.
  6. //
  7. // Copyright⌐ 2006 Pioneers of the Inevitable LLC
  8. // http://songbirdnest.com
  9. // 
  10. // This file may be licensed under the terms of of the
  11. // GNU General Public License Version 2 (the ôGPLö).
  12. // 
  13. // Software distributed under the License is distributed 
  14. // on an ôAS ISö basis, WITHOUT WARRANTY OF ANY KIND, either 
  15. // express or implied. See the GPL for the specific language 
  16. // governing rights and limitations.
  17. //
  18. // You should have received a copy of the GPL along with this 
  19. // program. If not, go to http://www.gnu.org/licenses/gpl.html
  20. // or write to the Free Software Foundation, Inc., 
  21. // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  22. // 
  23. // END SONGBIRD GPL
  24. //
  25.  */
  26.  
  27. //
  28. // sbIPlaylist Object
  29. //
  30.  
  31. const SONGBIRD_PLAYLISTMANAGER_CONTRACTID = "@songbird.org/Songbird/PlaylistManager;1";
  32. const SONGBIRD_PLAYLISTMANAGER_CLASSNAME = "Songbird PlaylistManager Interface"
  33. const SONGBIRD_PLAYLISTMANAGER_CID = Components.ID('{0BE3A41A-6673-494A-A53E-9740A98ACFF7}');
  34. const SONGBIRD_PLAYLISTMANAGER_IID = Components.interfaces.sbIPlaylistManager;
  35.  
  36. const PLAYLIST_TABLE_NAME = "__table__";
  37.  
  38. const SIMPLEPLAYLIST_LIST_TABLE_NAME = "simpleplaylist_list";
  39. const PLAYLIST_LIST_TABLE_NAME = "playlist_list";
  40. const DYNAMICPLAYLIST_LIST_TABLE_NAME = "dynamicplaylist_list";
  41. const SMARTPLAYLIST_LIST_TABLE_NAME = "smartplaylist_list";
  42.  
  43. const PLAYLIST_DESC_TABLE_CREATE = " (column_name TEXT UNIQUE NOT NULL, readable_name TEXT, is_visible INTEGER(0, 1) DEFAULT 0, default_visibility INTEGER(0, 1) DEFAULT 0, is_metadata INTEGER(0, 1) DEFAULT 0, sort_weight INTEGER DEFAULT 0, width INTEGER DEFAULT 0)";
  44. const PLAYLIST_TABLE_CREATE = " (playlist_id INTEGER PRIMARY KEY, playlist_uuid BLOB, playlist_service_uuid BLOB, playlist_order INTEGER DEFAULT -1, playlist_has_been_processed INTEGER(0,1) DEFAULT 0)";
  45.  
  46. const SIMPLEPLAYLIST_LIST_TABLE_CREATE = "CREATE TABLE simpleplaylist_list (service_uuid BLOB NOT NULL, name TEXT UNIQUE NOT NULL, readable_name TEXT NOT NULL, description TEXT DEFAULT '', type TEXT DEFAULT '', base_type TEXT DEFAULT 'simple')";
  47. const PLAYLIST_LIST_TABLE_CREATE = "CREATE TABLE playlist_list (service_uuid BLOB NOT NULL, name TEXT UNIQUE NOT NULL, readable_name TEXT NOT NULL, description TEXT DEFAULT '', type TEXT DEFAULT '', base_type TEXT DEFAULT 'playlist')";
  48. const DYNAMICPLAYLIST_LIST_TABLE_CREATE = "CREATE TABLE dynamicplaylist_list (service_uuid BLOB NOT NULL, name TEXT UNIQUE NOT NULL, readable_name TEXT NOT NULL, url TEXT NOT NULL UNIQUE, periodicity INTEGER NOT NULL DEFAULT 0, description TEXT DEFAULT '', type TEXT DEFAULT '', last_update INTEGER DEFAULT 0, base_type TEXT DEFAULT 'dynamic')";
  49.  
  50. const SMARTPLAYLIST_LIST_TABLE_CREATE = "CREATE TABLE smartplaylist_list (service_uuid BLOB NOT NULL, name BLOB PRIMARY KEY NOT NULL, readable_name TEXT NOT NULL, description TEXT DEFAULT '', library BLOB NOT NULL, limit_value INTEGER NOT NULL DEFAULT 0, limit_type TEXT DEFAULT '', selected_by TEXT DEFAULT '', match_on TEXT DEFAULT '', query TEXT DEFAULT '', type TEXT DEFAULT '', base_type TEXT DEFAULT 'smart')";
  51. const SMARTPLAYLIST_CONSTRAINT_LIST_TABLE_CREATE = " (id INTEGER PRIMARY KEY, column_name TEXT NOT NULL DEFAULT '', constraint_name TEXT NOT NULL DEFAULT '', constraint_value TEXT NOT NULL DEFAULT '')";
  52.  
  53. function CPlaylistManager()
  54. {
  55. }
  56.  
  57. /* I actually need a constructor in this case. */
  58. CPlaylistManager.prototype.constructor = CPlaylistManager;
  59.  
  60. /* the CPlaylistManager class def */
  61. CPlaylistManager.prototype = 
  62. {
  63.   CreateDefaultPlaylistManager: function(queryObj)
  64.   {
  65.     if(queryObj != null)
  66.     {
  67.       queryObj.ResetQuery();
  68.       queryObj.AddQuery(SIMPLEPLAYLIST_LIST_TABLE_CREATE);
  69.       queryObj.AddQuery(PLAYLIST_LIST_TABLE_CREATE);
  70.       queryObj.AddQuery(DYNAMICPLAYLIST_LIST_TABLE_CREATE);
  71.       queryObj.AddQuery(SMARTPLAYLIST_LIST_TABLE_CREATE);
  72.       
  73.       queryObj.Execute();
  74.       queryObj.WaitForCompletion();
  75.     }
  76.     
  77.     return;
  78.   },
  79.   
  80.   CreateSimplePlaylist: function(strName, strReadableName, strDescription, strType, nMetaFieldCount, aMetaFields, queryObj)
  81.   {
  82.     const SimplePlaylist = new Components.Constructor("@songbird.org/Songbird/SimplePlaylist;1", "sbISimplePlaylist");
  83.  
  84.     if(queryObj != null)
  85.     {
  86.       queryObj.ResetQuery();
  87.  
  88.       var strQuery = "CREATE TABLE \"" + strName + "_desc\"" + PLAYLIST_DESC_TABLE_CREATE;
  89.       queryObj.AddQuery(strQuery);
  90.       
  91.       var i = 0;
  92.       strQuery = "CREATE TABLE \"" + strName + "\" (id INTEGER PRIMARY KEY AUTOINCREMENT, url TEXT UNIQUE NOT NULL ";
  93.       
  94.       for(i = 0; i < nMetaFieldCount; ++i)
  95.       {
  96.         strQuery += ", " + aMetaFields[i] + " TEXT ";
  97.           
  98.         queryObj.AddQuery("INSERT OR REPLACE INTO \"" + strName + "_desc\" (column_name) VALUES (\"" + aMetaFields[i] + "\")");
  99.       }
  100.       
  101.       strQuery += ")";
  102.       
  103.       strReadableName = strReadableName.replace(/"/g, "\"\"");
  104.       strDescription = strDescription.replace(/"/g, "\"\"");
  105.       
  106.       queryObj.AddQuery(strQuery);
  107.       queryObj.AddQuery("INSERT INTO " + SIMPLEPLAYLIST_LIST_TABLE_NAME + " (service_uuid, name, readable_name, description, type) VALUES (\"" + queryObj.GetDatabaseGUID() + "\", \"" + strName + "\", \"" + strReadableName + "\", \""+ strDescription + "\", \"" + strType + "\")");
  108.       
  109.       queryObj.Execute();
  110.       queryObj.WaitForCompletion();
  111.       
  112.       var simplePlaylist = (new SimplePlaylist()).QueryInterface(Components.interfaces.sbISimplePlaylist);
  113.       
  114.       simplePlaylist.SetName(strName);
  115.       simplePlaylist.SetQueryObject(queryObj);
  116.       
  117.       return simplePlaylist;      
  118.     }
  119.     
  120.     return null;
  121.   },
  122.   
  123.   CreatePlaylist: function(strName, strReadableName, strDescription, strType, queryObj)
  124.   {
  125.     const Playlist = new Components.Constructor("@songbird.org/Songbird/Playlist;1", "sbIPlaylist");
  126.     
  127.     if(queryObj != null)
  128.     {
  129.       queryObj.ResetQuery();
  130.       
  131.       var strQuery = "CREATE TABLE \"" + strName + "\"" + PLAYLIST_TABLE_CREATE;
  132.       strReadableName = strReadableName.replace(/\"./, "\"\"");
  133.       strDescription = strDescription.replace(/\"./, "\"\"");
  134.  
  135.       queryObj.AddQuery(strQuery);
  136.       queryObj.AddQuery("INSERT OR REPLACE INTO " + PLAYLIST_LIST_TABLE_NAME + " (service_uuid, name, readable_name, description, type) VALUES (\"" + queryObj.GetDatabaseGUID() + "\", \"" + strName + "\", \"" + strReadableName + "\", \"" + strDescription + "\", \"" + strType + "\")");
  137.  
  138.       strQuery = "CREATE TABLE \"" + strName + "_desc\"" + PLAYLIST_DESC_TABLE_CREATE;
  139.       queryObj.AddQuery(strQuery);
  140.       
  141.       queryObj.Execute();
  142.       queryObj.WaitForCompletion();
  143.       
  144.       var playlist = (new Playlist()).QueryInterface(Components.interfaces.sbIPlaylist);
  145.       
  146.       playlist.SetName(strName);
  147.       playlist.SetQueryObject(queryObj);
  148.       
  149.       return playlist;
  150.     }
  151.     
  152.     return null;
  153.   },
  154.  
  155.   CreateDynamicPlaylist: function(strName, strReadableName, strDescription, strType, strURL, nPeriodicity, queryObj)
  156.   {
  157.     const DynamicPlaylist = new Components.Constructor("@songbird.org/Songbird/DynamicPlaylist;1", "sbIDynamicPlaylist");
  158.     
  159.     if(queryObj != null)
  160.     {
  161.       queryObj.ResetQuery();
  162.       
  163.       var strQuery = "CREATE TABLE \"" + strName + "\"" + PLAYLIST_TABLE_CREATE;
  164.  
  165.       strReadableName = strReadableName.replace(/"/g, "\"\"");
  166.       strDescription = strDescription.replace(/"/g, "\"\"");
  167.  
  168.       queryObj.AddQuery(strQuery);
  169.       queryObj.AddQuery("INSERT OR REPLACE INTO " + DYNAMICPLAYLIST_LIST_TABLE_NAME + " (service_uuid, name, readable_name, url, periodicity, description, type) VALUES (\"" + queryObj.GetDatabaseGUID() + "\", \"" + strName + "\", \"" + strReadableName + "\", \"" + strURL + "\", \"" + nPeriodicity + "\", \""+ strDescription + "\", \"" + strType + "\")");
  170.  
  171.       strQuery = "CREATE TABLE \"" + strName + "_desc\"" + PLAYLIST_DESC_TABLE_CREATE;
  172.       queryObj.AddQuery(strQuery);
  173.       
  174.       queryObj.Execute();
  175.       queryObj.WaitForCompletion();
  176.       
  177.       var playlist = (new DynamicPlaylist()).QueryInterface(Components.interfaces.sbIDynamicPlaylist);
  178.       
  179.       playlist.SetName(strName);
  180.       playlist.SetQueryObject(queryObj);
  181.       
  182.       return playlist;
  183.     }
  184.     
  185.     return null;
  186.   },
  187.   
  188.   CreateSmartPlaylist: function(strName, strReadableName, strDescription, strType, strLibrary, nLimit, strLimitType, strSelectedBy, strMatchOn, queryObj)
  189.   {
  190.     const SmartPlaylist = new Components.Constructor("@songbird.org/Songbird/SmartPlaylist;1", "sbISmartPlaylist");
  191.     
  192.     if(queryObj != null)
  193.     {
  194.       queryObj.ResetQuery();
  195.  
  196.       strReadableName = strReadableName.replace(/"/g, "\"\"");
  197.       strDescription = strDescription.replace(/"/g, "\"\"");
  198.       
  199.       strQuery = "INSERT OR REPLACE INTO " + SMARTPLAYLIST_LIST_TABLE_NAME + " (service_uuid, name, readable_name, description, library, limit_value, limit_type, selected_by, match_on, type) VALUES (\"";
  200.       strQuery += queryObj.GetDatabaseGUID() + "\", \"";
  201.       strQuery += strName + "\", \"";
  202.       strQuery += strReadableName + "\", \"";
  203.       strQuery += strDescription + "\", \"";
  204.       strQuery += strLibrary + "\", \"";
  205.       strQuery += nLimit + "\", \"";
  206.       strQuery += strLimitType + "\", \"";
  207.       strQuery += strSelectedBy + "\", \"";
  208.       strQuery += strMatchOn + "\", \"";
  209.       strQuery += strType + "\")";
  210.       queryObj.AddQuery(strQuery);
  211.  
  212.       strQuery = "CREATE TABLE \"" + strName + "_constraints\"" + SMARTPLAYLIST_CONSTRAINT_LIST_TABLE_CREATE;
  213.       queryObj.AddQuery(strQuery);
  214.       
  215.       queryObj.Execute();
  216.       queryObj.WaitForCompletion();
  217.       
  218.       var playlist = (new SmartPlaylist()).QueryInterface(Components.interfaces.sbISmartPlaylist);
  219.       
  220.       playlist.SetName(strName);
  221.       playlist.SetQueryObject(queryObj);
  222.       
  223.       return playlist;
  224.     }
  225.     
  226.     return null;
  227.   },
  228.   
  229.   CopySimplePlaylist: function(strSourceDB, strSourceName, strSourceFilterColumn, nSourceFilterValueCount, aSourceFilterValues, strDestDB, strDestName, strReadableName, strDescription, strType, queryObj)
  230.   {
  231.     const SimplePlaylist = new Components.Constructor("@songbird.org/Songbird/SimplePlaylist;1", "sbISimplePlaylist");
  232.     
  233.     if(queryObj != null)
  234.     {
  235.       var bSrcIsSimple = true;
  236.       queryObj.SetDatabaseGUID(strSourceDB);
  237.       var pSourcePlaylist = this.GetSimplePlaylist(strSourceName, queryObj);
  238.      
  239.       if(pSourcePlaylist == null)
  240.       {
  241.         pSourcePlaylist = this.GetPlaylist(strSourceName, queryObj);
  242.         
  243.         if(pSourcePlaylist)
  244.         {
  245.           bSrcIsSimple = false;
  246.         }
  247.       }
  248.       
  249.       if(pSourcePlaylist)
  250.       {
  251.         pSourcePlaylist.GetTableInfo();
  252.         
  253.         var resObj = queryObj.GetResultObject();
  254.       
  255.         var i = 0;
  256.         var rowCount = resObj.GetRowCount();
  257.         var strColName = "", strColType = "", columnList = "";
  258.         var aCols = new Array;
  259.         var aColType = new Array;
  260.         
  261.         for(i = 0; i < rowCount; i++)
  262.         {
  263.           strColName = resObj.GetRowCell(i, 1);
  264.           strColType = resObj.GetRowCell(i, 2);
  265.          
  266.           if(strColName != "playlist_id") 
  267.           {
  268.             aCols.push(strColName);
  269.             aColType.push(strColType);
  270.             
  271.             columnList += strColName;
  272.             
  273.             if(i + 1 != rowCount)
  274.               columnList += ", ";
  275.           }
  276.         }
  277.  
  278.         queryObj.ResetQuery();
  279.         queryObj.SetDatabaseGUID(strDestDB);
  280.         
  281.         this.CreateDefaultPlaylistManager(queryObj);
  282.         
  283.         var pDestPlaylist = this.GetSimplePlaylist(strDestName, queryObj);
  284.         
  285.         if(!pDestPlaylist)
  286.         {
  287.           pDestPlaylist = this.CreateSimplePlaylist(strDestName, strReadableName, strDescription, strType, 0, null, queryObj);
  288.  
  289.           if(pDestPlaylist)
  290.           {
  291.             for(i = 0; i < rowCount; i++)
  292.             {
  293.               pDestPlaylist.AddColumn(aCols[i], aColType[i]);
  294.             }
  295.           }
  296.         } 
  297.  
  298.         queryObj.ResetQuery();
  299.         queryObj.SetDatabaseGUID(strSourceDB);
  300.         
  301.         queryObj.AddQuery("ATTACH DATABASE \"db/" + strDestDB + "\" AS \"" + strDestDB + "\"");
  302.  
  303.         var strSimpleQuery = "INSERT INTO \"" + strDestDB + "\".\"" + strDestName + "\" (" + columnList + ") SELECT ";
  304.         strSimpleQuery += columnList + " FROM \"" + strSourceName + "\"";
  305.         
  306.         var strQuery = "INSERT INTO \"" + strDestDB + "\".\"" + strDestName + "\" (url ";
  307.         if(rowCount) strQuery += ", ";
  308.         strQuery += columnList + ") SELECT url ";
  309.         if(rowCount) strQuery += ", ";
  310.         strQuery += columnList + " FROM \"" + strSourceName + "\" LEFT JOIN library ON \"" + strSourceName + "\".playlist_uuid = library.uuid";
  311.         
  312.         var strFilterQuery = " WHERE ";
  313.         
  314.         if(strSourceFilterColumn != "" && nSourceFilterValueCount && aSourceFilterValues)
  315.         {
  316.           strFilterQuery += "playlist_" + strSourceFilterColumn + " IN (";
  317.           for(var i = 0; i < nSourceFilterValueCount; ++i)
  318.           {
  319.             strFilterQuery += "\"" + aSourceFilterValues[i] + "\"";
  320.             if(i + 1 != nSourceFilterValueCount)
  321.               strFilterQuery += ", ";
  322.           }
  323.           strFilterQuery += ")";
  324.           
  325.           strSimpleQuery += strFilterQuery;
  326.           strQuery += strFilterQuery;
  327.         }
  328.  
  329.         if(bSrcIsSimple)
  330.         {
  331.           queryObj.AddQuery(strSimpleQuery);
  332.         }
  333.         else
  334.         {
  335.           queryObj.AddQuery(strQuery);
  336.         }
  337.  
  338.         queryObj.AddQuery("DETACH DATABASE \"" + strDestDB + "\"");
  339.           
  340.         var success = queryObj.Execute();
  341.         queryObj.WaitForCompletion();
  342.         
  343.         var error = queryObj.GetLastError();
  344.         queryObj.SetDatabaseGUID(strDestDB);
  345.       
  346.         return pDestPlaylist;
  347.       }
  348.     }
  349.     
  350.     return null;
  351.   },
  352.  
  353.   CopyPlaylist: function(strSourceDB, strSourceName, strDestDB, strDestName, strReadableName, strDescription, strType, queryObj)
  354.   {
  355.     const Playlist = new Components.Constructor("@songbird.org/Songbird/Playlist;1", "sbIPlaylist");
  356.  
  357.     if(queryObj != null)
  358.     {
  359.       queryObj.SetDatabaseGUID(strSourceDB);
  360.       var pSourcePlaylist = this.GetSimplePlaylist(strSourceName, queryObj);
  361.       
  362.       if(pSourcePlaylist)
  363.       {
  364.         pSourcePlaylist.GetTableInfo();
  365.         var resObj = queryObj.GetResultObject();
  366.  
  367.         queryObj.SetDatabaseGUID(strDestDB);
  368.         var pDestPlaylist = this.CreatePlaylist(strDestName, strReadableName, strDescription, strType, queryObj);
  369.         
  370.         if(pDestPlaylist)
  371.         {
  372.         
  373.           var i = 0;
  374.           var rowCount = resObj.GetRowCount();
  375.           var columnList = "";
  376.           
  377.           for(i = 0; i < rowCount; i++)
  378.           {
  379.             var strColName = resObj.GetRowCellByColumn(i, "name");
  380.             var strColType = resObj.GetRowCellByColumn(i, "type");
  381.             
  382.             columnList +=  strColName;
  383.             if(i != rowCount - 1)
  384.               columnList += ", ";
  385.               
  386.             pDestPlaylist.AddColumn(strColName, strColType);
  387.           }
  388.        
  389.           queryObj.ResetQuery();
  390.           queryObj.SetDatabaseGUID(strSourceDB);
  391.           
  392.           queryObj.AddQuery("INSERT INTO \"db/" + strDestDB + "\".\"" + strDestName + "\" (" + columnList + ") SELECT " + columnList + " FROM \"" + strSourceName + "\"");
  393.           queryObj.Execute();
  394.           queryObj.WaitForCompletion();
  395.           
  396.           queryObj.SetDatabaseGUID(strDestDB);
  397.        
  398.           return pDestPlaylist;
  399.         }
  400.       }
  401.     }
  402.     
  403.     return null;
  404.   },
  405.  
  406.   DeleteSimplePlaylist: function(strName, queryObj)
  407.   {
  408.     if(queryObj != null)
  409.     {
  410.       queryObj.ResetQuery();
  411.       queryObj.AddQuery("SELECT name FROM " + SIMPLEPLAYLIST_LIST_TABLE_NAME + " WHERE name = \"" + strName + "\"");
  412.       
  413.       queryObj.Execute();
  414.       queryObj.WaitForCompletion();
  415.       
  416.       var resObj = queryObj.GetResultObject();
  417.       
  418.       if(resObj.GetRowCount() > 0)
  419.       {
  420.         queryObj.AddQuery("DROP TABLE \"" + strName + "\"");
  421.         queryObj.AddQuery("DELETE FROM " + SIMPLEPLAYLIST_LIST_TABLE_NAME + " WHERE name = \"" + strName + "\"");
  422.         queryObj.Execute();
  423.         queryObj.WaitForCompletion();
  424.         
  425.         return 1;
  426.       }
  427.     }
  428.     
  429.     return 0;
  430.   },
  431.   
  432.   DeletePlaylist: function(strName, queryObj)
  433.   {
  434.     if(queryObj != null)
  435.     {
  436.       queryObj.ResetQuery();
  437.       queryObj.AddQuery("SELECT name FROM " + PLAYLIST_LIST_TABLE_NAME + " WHERE name = \"" + strName + "\"");
  438.       
  439.       queryObj.Execute();
  440.       queryObj.WaitForCompletion();
  441.       
  442.       var resObj = queryObj.GetResultObject();
  443.       
  444.       if(resObj.GetRowCount() > 0)
  445.       {
  446.         queryObj.AddQuery("DROP TABLE \"" + strName + "\"");
  447.         queryObj.AddQuery("DELETE FROM " + PLAYLIST_LIST_TABLE_NAME + " WHERE name = \"" + strName + "\"");
  448.         queryObj.Execute();
  449.         queryObj.WaitForCompletion();
  450.         
  451.         return 1;
  452.       }
  453.     }
  454.  
  455.     return 0;
  456.   },
  457.  
  458.   DeleteDynamicPlaylist: function(strName, queryObj)
  459.   {
  460.     if(queryObj != null)
  461.     {
  462.       queryObj.ResetQuery();
  463.       queryObj.AddQuery("SELECT name FROM " + DYNAMICPLAYLIST_LIST_TABLE_NAME + " WHERE name = \"" + strName + "\"");
  464.       
  465.       queryObj.Execute();
  466.       queryObj.WaitForCompletion();
  467.       
  468.       var resObj = queryObj.GetResultObject();
  469.       
  470.       if(resObj.GetRowCount() > 0)
  471.       {
  472.         queryObj.AddQuery("DROP TABLE \"" + strName + "\"");
  473.         queryObj.AddQuery("DELETE FROM " + DYNAMICPLAYLIST_LIST_TABLE_NAME + " WHERE name = \"" + strName + "\"");
  474.         queryObj.Execute();
  475.         queryObj.WaitForCompletion();
  476.         
  477.         return 1;
  478.       }
  479.     }
  480.  
  481.     return 0;
  482.   },
  483.   
  484.   DeleteSmartPlaylist: function(strName, queryObj)
  485.   {
  486.     if(queryObj != null)
  487.     {
  488.       queryObj.ResetQuery();
  489.       queryObj.AddQuery("SELECT name FROM " + SMARTPLAYLIST_LIST_TABLE_NAME + " WHERE name = \"" + strName + "\"");
  490.       
  491.       queryObj.Execute();
  492.       queryObj.WaitForCompletion();
  493.       
  494.       var resObj = queryObj.GetResultObject();
  495.       
  496.       if(resObj.GetRowCount() > 0)
  497.       {
  498.         queryObj.AddQuery("DROP TABLE \"" + strName + "\"");
  499.         queryObj.AddQuery("DROP TABLE \"" + strName + "_constraints\"");
  500.         queryObj.AddQuery("DELETE FROM " + SMARTPLAYLIST_LIST_TABLE_NAME + " WHERE name = \"" + strName + "\"");
  501.         
  502.         queryObj.Execute();
  503.         queryObj.WaitForCompletion();
  504.         
  505.         return 1;
  506.       }
  507.     }
  508.  
  509.     return 0;
  510.   },
  511.   
  512.   GetAllPlaylistList: function(queryObj)
  513.   {
  514.     if(queryObj != null)
  515.     {
  516.       queryObj.ResetQuery();
  517.       queryObj.SetDatabaseGUID("*");
  518.       
  519.       queryObj.AddQuery("SELECT service_uuid, name, readable_name, type, base_type, description FROM " + SIMPLEPLAYLIST_LIST_TABLE_NAME);
  520.       queryObj.AddQuery("SELECT service_uuid, name, readable_name, type, base_type, description FROM " + PLAYLIST_LIST_TABLE_NAME);
  521.       queryObj.AddQuery("SELECT service_uuid, name, readable_name, type, base_type, description FROM " + DYNAMICPLAYLIST_LIST_TABLE_NAME);
  522.       queryObj.AddQuery("SELECT service_uuid, name, readable_name, type, base_type, description FROM " + SMARTPLAYLIST_LIST_TABLE_NAME);
  523.       
  524.       queryObj.Execute();
  525.       
  526.       if(!queryObj.IsAyncQuery());
  527.         queryObj.WaitForCompletion();
  528.     }
  529.   },
  530.  
  531.   GetSimplePlaylistList: function(queryobj)
  532.   {
  533.     if(queryObj != null)
  534.     {
  535.       queryObj.ResetQuery();
  536.       queryObj.AddQuery("SELECT service_uuid, name, readable_name, type, base_type, description FROM " + SIMPLEPLAYLIST_LIST_TABLE_NAME);
  537.       
  538.       queryObj.Execute();
  539.       if(!queryObj.IsAyncQuery())      
  540.         queryObj.WaitForCompletion();
  541.     }
  542.   },
  543.   
  544.   GetPlaylistList: function(queryObj)
  545.   {
  546.     if(queryObj != null)
  547.     {
  548.       queryObj.ResetQuery();
  549.       queryObj.AddQuery("SELECT service_uuid, name, readable_name, type, base_type, description FROM " + PLAYLIST_LIST_TABLE_NAME);
  550.       
  551.       queryObj.Execute();
  552.       if(!queryObj.IsAyncQuery())      
  553.         queryObj.WaitForCompletion();
  554.     }
  555.   },
  556.  
  557.   GetDynamicPlaylistList: function(queryObj)
  558.   {
  559.     if(queryObj != null)
  560.     {
  561.       queryObj.ResetQuery();
  562.       queryObj.AddQuery("SELECT service_uuid, name, readable_name, type, base_type, description FROM " + DYNAMICPLAYLIST_LIST_TABLE_NAME);
  563.       
  564.       queryObj.Execute();
  565.       if(!queryObj.IsAyncQuery())      
  566.         queryObj.WaitForCompletion();
  567.     }
  568.   },
  569.   
  570.   GetSmartPlaylistList: function(queryObj)
  571.   {
  572.     if(queryObj != null)
  573.     {
  574.       queryObj.ResetQuery();
  575.       queryObj.AddQuery("SELECT service_uuid, name, readable_name, type, base_type, description FROM " + SMARTPLAYLIST_LIST_TABLE_NAME);
  576.       
  577.       queryObj.Execute();
  578.       if(!queryObj.IsAyncQuery())
  579.         queryObj.WaitForCompletion();
  580.     }
  581.   },
  582.  
  583.   GetSimplePlaylist: function(strName, queryObj)
  584.   {
  585.     const SimplePlaylist = new Components.Constructor("@songbird.org/Songbird/SimplePlaylist;1", "sbISimplePlaylist");
  586.  
  587.     if(queryObj != null)
  588.     {
  589.       queryObj.ResetQuery();
  590.       queryObj.AddQuery("SELECT name FROM " + SIMPLEPLAYLIST_LIST_TABLE_NAME + " WHERE name = \"" + strName + "\"");
  591.       
  592.       queryObj.Execute();
  593.       queryObj.WaitForCompletion();
  594.  
  595.       if(queryObj.GetResultObject().GetRowCount() > 0)
  596.       {
  597.         var simplePlaylist = (new SimplePlaylist()).QueryInterface(Components.interfaces.sbISimplePlaylist);
  598.         
  599.         simplePlaylist.SetName(strName);
  600.         simplePlaylist.SetQueryObject(queryObj);
  601.         
  602.         return simplePlaylist;
  603.       }
  604.     }
  605.  
  606.     return null;
  607.   },
  608.   
  609.   GetPlaylist: function(strName, queryObj)
  610.   {
  611.     const Playlist = new Components.Constructor("@songbird.org/Songbird/Playlist;1", "sbIPlaylist");
  612.  
  613.     if(queryObj != null)
  614.     {
  615.       queryObj.ResetQuery();
  616.       queryObj.AddQuery("SELECT name FROM " + PLAYLIST_LIST_TABLE_NAME + " WHERE name = \"" + strName + "\"");
  617.       
  618.       queryObj.Execute();
  619.       queryObj.WaitForCompletion();
  620.  
  621.       var playlist = null;
  622.       if(queryObj.GetResultObject().GetRowCount() > 0)
  623.       {
  624.         playlist = (new Playlist()).QueryInterface(Components.interfaces.sbIPlaylist);
  625.         
  626.         playlist.SetName(strName);
  627.         playlist.SetQueryObject(queryObj);
  628.       
  629.         return playlist;
  630.       }
  631.       else
  632.       {
  633.         playlist = this.GetDynamicPlaylist(strName, queryObj);
  634.         if(playlist)
  635.         {
  636.           return playlist.QueryInterface(Components.interfaces.sbIPlaylist);
  637.         }
  638.         
  639.         playlist = this.GetSmartPlaylist(strName, queryObj);
  640.         if(playlist)
  641.           return playlist.QueryInterface(Components.interfaces.sbIPlaylist);
  642.       }
  643.     }
  644.  
  645.     return null;
  646.   },
  647.  
  648.   GetDynamicPlaylist: function(strName, queryObj)
  649.   {
  650.     const DynamicPlaylist = new Components.Constructor("@songbird.org/Songbird/DynamicPlaylist;1", "sbIDynamicPlaylist");
  651.  
  652.     if(queryObj != null)
  653.     {
  654.       queryObj.ResetQuery();
  655.       queryObj.AddQuery("SELECT name FROM " + DYNAMICPLAYLIST_LIST_TABLE_NAME + " WHERE name = \"" + strName + "\"");
  656.       
  657.       queryObj.Execute();
  658.       queryObj.WaitForCompletion();
  659.  
  660.       var resObj = queryObj.GetResultObject();
  661.       if(resObj.GetRowCount() > 0)
  662.       {
  663.         var playlist = new DynamicPlaylist();
  664.         if(playlist)
  665.         {
  666.           playlist = playlist.QueryInterface(Components.interfaces.sbIDynamicPlaylist);
  667.           playlist.SetName(strName);
  668.           playlist.SetQueryObject(queryObj);
  669.         
  670.           return playlist;
  671.         }
  672.       }
  673.     }
  674.  
  675.     return null;
  676.   },
  677.   
  678.   GetSmartPlaylist: function(strName, queryObj)
  679.   {
  680.     const SmartPlaylist = new Components.Constructor("@songbird.org/Songbird/SmartPlaylist;1", "sbISmartPlaylist");
  681.  
  682.     if(queryObj != null)
  683.     {
  684.       queryObj.ResetQuery();
  685.       queryObj.AddQuery("SELECT name FROM " + SMARTPLAYLIST_LIST_TABLE_NAME + " WHERE name = \"" + strName + "\"");
  686.  
  687.       queryObj.Execute();
  688.       queryObj.WaitForCompletion();
  689.  
  690.       var resObj = queryObj.GetResultObject();
  691.       if(resObj.GetRowCount() > 0)
  692.       {
  693.         var playlist = (new SmartPlaylist()).QueryInterface(Components.interfaces.sbISmartPlaylist);
  694.         
  695.         playlist.SetName(strName);
  696.         playlist.SetQueryObject(queryObj);
  697.       
  698.         return playlist;
  699.       }
  700.     }
  701.  
  702.     return null;
  703.   },
  704.  
  705.   GetDynamicPlaylistsForUpdate: function(queryObj)
  706.   {
  707.     if(queryObj != null)
  708.     {
  709.       queryObj.SetDatabaseGUID("*");
  710.       
  711.       var strQuery = "SELECT service_uuid, name, periodicity, last_update FROM " + DYNAMICPLAYLIST_LIST_TABLE_NAME;
  712.       queryObj.ResetQuery();
  713.       queryObj.AddQuery(strQuery);
  714.       
  715.       queryObj.Execute();
  716.       queryObj.WaitForCompletion();
  717.       
  718.       var resObj = queryObj.GetResultObject();
  719.       
  720.       var dNow = new Date();
  721.       var playlists = new Array();
  722.       
  723.       for(var i = 0; i < resObj.GetRowCount(); ++i)
  724.       {
  725.         var lastUpdate = parseInt(resObj.GetRowCellByColumn(i, "last_update"));
  726.         var periodicity = parseInt(resObj.GetRowCellByColumn(i, "periodicity"));
  727.         
  728.         if(dNow.getTime() - lastUpdate >= (periodicity * 1000 * 60))
  729.         {
  730.           var name = resObj.GetRowCellByColumn(i, "name");
  731.           playlists.push(name);
  732.         }
  733.       }      
  734.       
  735.       if(playlists.length)
  736.       {
  737.         strQuery = "SELECT * FROM " + DYNAMICPLAYLIST_LIST_TABLE_NAME + " WHERE name IN (";
  738.         for(i = 0; i < playlists.length; ++i)
  739.         {
  740.           strQuery += "\"" + playlists[i] + "\"";
  741.           if(i + 1 != playlists.length)
  742.             strQuery += ", ";
  743.         }
  744.         
  745.         strQuery += ")";
  746.  
  747.         queryObj.ResetQuery();
  748.         queryObj.AddQuery(strQuery);
  749.         
  750.         queryObj.Execute();
  751.         queryObj.WaitForCompletion();
  752.         
  753.         resObj = queryObj.GetResultObject();
  754.         return parseInt(resObj.GetRowCount());
  755.       }
  756.     }
  757.     
  758.     return 0;
  759.   },
  760.  
  761.   SetDynamicPlaylistLastUpdate: function(strName, queryObj)
  762.   {
  763.     if(queryObj != null)
  764.     {
  765.       var dNow = new Date();
  766.       
  767.       queryObj.ResetQuery();
  768.       queryObj.AddQuery("UPDATE \"" + DYNAMICPLAYLIST_LIST_TABLE_NAME + "\" SET last_update = " + dNow.getTime() + " WHERE name = \"" + strName + "\"");
  769.       
  770.       queryObj.Execute();
  771.       queryObj.WaitForCompletion();
  772.       
  773.       return queryObj.GetLastError() ? false : true;
  774.     }
  775.     
  776.     return false;
  777.   },
  778.  
  779.   PurgeTrackByGUIDFromPlaylists: function(mediaGUID, strDBGUID)
  780.   {
  781.     var queryObj = Components.classes["@songbird.org/Songbird/DatabaseQuery;1"].createInstance(Components.interfaces.sbIDatabaseQuery);
  782.     if(queryObj)
  783.     {
  784.       queryObj.SetAsyncQuery(false);
  785.       queryObj.SetDatabaseGUID(strDBGUID);
  786.       
  787.       var strQuery = "SELECT name FROM " + PLAYLIST_LIST_TABLE_NAME + " UNION ";
  788.       strQuery += "SELECT name FROM " + DYNAMICPLAYLIST_LIST_TABLE_NAME;
  789.       
  790.       queryObj.AddQuery(strQuery);
  791.       queryObj.Execute();
  792.       
  793.       var resObj = queryObj.GetResultObject();
  794.       var rowCount = resObj.GetRowCount();
  795.       
  796.       var q = Components.classes["@songbird.org/Songbird/DatabaseQuery;1"].createInstance(Components.interfaces.sbIDatabaseQuery);
  797.       q.SetAsyncQuery(false);
  798.       q.SetDatabaseGUID(strDBGUID);
  799.       
  800.       for(var i = 0; i < rowCount; i++)
  801.       {
  802.         var plName = resObj.GetRowCellByColumn(i, "name");
  803.         q.AddQuery("DELETE FROM \"" + plName + "\" WHERE playlist_uuid = \"" + mediaGUID + "\"");
  804.       }
  805.       
  806.       q.Execute();
  807.     }
  808.   },
  809.  
  810.   QueryInterface: function(iid)
  811.   {
  812.       if (!iid.equals(Components.interfaces.nsISupports) &&
  813.           !iid.equals(SONGBIRD_PLAYLISTMANAGER_IID))
  814.           throw Components.results.NS_ERROR_NO_INTERFACE;
  815.       return this;
  816.   }
  817. };
  818.  
  819. /**
  820.  * \class sbPlaylistManagerModule
  821.  * \brief 
  822.  */
  823. var sbPlaylistManagerModule = 
  824. {
  825.   registerSelf: function(compMgr, fileSpec, location, type)
  826.   {
  827.       compMgr = compMgr.QueryInterface(Components.interfaces.nsIComponentRegistrar);
  828.       compMgr.registerFactoryLocation(SONGBIRD_PLAYLISTMANAGER_CID, 
  829.                                       SONGBIRD_PLAYLISTMANAGER_CLASSNAME, 
  830.                                       SONGBIRD_PLAYLISTMANAGER_CONTRACTID, 
  831.                                       fileSpec, 
  832.                                       location,
  833.                                       type);
  834.   },
  835.  
  836.   getClassObject: function(compMgr, cid, iid) 
  837.   {
  838.       if (!cid.equals(SONGBIRD_PLAYLISTMANAGER_CID))
  839.           throw Components.results.NS_ERROR_NO_INTERFACE;
  840.  
  841.       if (!iid.equals(Components.interfaces.nsIFactory))
  842.           throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
  843.  
  844.       return sbPlaylistManagerFactory;
  845.   },
  846.  
  847.   canUnload: function(compMgr)
  848.   { 
  849.     return true; 
  850.   }
  851. };
  852.  
  853. /**
  854.  * \class sbPlaylistManagerFactory
  855.  * \brief 
  856.  */
  857. var sbPlaylistManagerFactory =
  858. {
  859.     createInstance: function(outer, iid)
  860.     {
  861.         if (outer != null)
  862.             throw Components.results.NS_ERROR_NO_AGGREGATION;
  863.     
  864.         if (!iid.equals(SONGBIRD_PLAYLISTMANAGER_IID) &&
  865.             !iid.equals(Components.interfaces.nsISupports))
  866.             throw Components.results.NS_ERROR_INVALID_ARG;
  867.  
  868.         return (new CPlaylistManager()).QueryInterface(iid);
  869.     }
  870. } //sbPlaylistFactory
  871.  
  872. /**
  873.  * \function NSGetModule
  874.  * \brief 
  875.  */
  876. function NSGetModule(comMgr, fileSpec)
  877.   return sbPlaylistManagerModule;
  878. } //NSGetModule